package com.heima.wemedia.config;

import com.heima.common.constants.rabbit.RabbitArticlePublishConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeclareRabbitArticlePublishConfig {
    @Bean
    public TopicExchange articlePublishExchange(){
        return new TopicExchange(RabbitArticlePublishConstants.ARTICLE_PUBLISH_EXCHANGE, true,false);
    }

    @Bean
    public Queue articlePublishQueue(){
        return QueueBuilder.durable(RabbitArticlePublishConstants.ARTICLE_PUBLISH_QUEUE)
                        .deadLetterExchange(RabbitArticlePublishConstants.ARTICLE_PUBLISH_DEAD_EXCHANGE)
                        .deadLetterRoutingKey(RabbitArticlePublishConstants.ARTICLE_PUBLISH_DEAD_ROUTING_KEY)
                        .build();
    }

    @Bean
    public TopicExchange articlePublishDeadExchange(){
        return new TopicExchange(RabbitArticlePublishConstants.ARTICLE_PUBLISH_DEAD_EXCHANGE, true, false);
    }

    @Bean
    public Queue articlePublishDeadQueue(){
        return QueueBuilder.durable(RabbitArticlePublishConstants.ARTICLE_PUBLISH_DEAD_QUEUE).build();
    }

    @Bean
    public Binding bindingArticlePublishQueue(){
        return BindingBuilder.bind(articlePublishQueue())
                .to(articlePublishExchange())
                .with(RabbitArticlePublishConstants.ARTICLE_PUBLISH_ROUTING_KEY);
    }

    @Bean
    public Binding bindingArticlePublishDeadQueue(){
        return BindingBuilder.bind(articlePublishDeadQueue())
                .to(articlePublishDeadExchange())
                .with(RabbitArticlePublishConstants.ARTICLE_PUBLISH_DEAD_ROUTING_KEY);
    }
}
